﻿using System;
using System.Collections.Generic;
using Opalis.QuickIntegrationKit;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Collections;

namespace Active_Directory
{
    [OpalisObject("Delete AD Computer", ShowFilters = false)]
    class DeleteComputer : IOpalisObject
    {
        private ConnectionCredentials credentials;

        [OpalisConfiguration]
        public ConnectionCredentials Credentials
        {
            get { return credentials; }
            set { credentials = value; }
        }

        public void Design(IOpalisDesigner designer)
        {
            designer.AddInput("Comptuer LDAP Path").WithDefaultValue("LDAP://CN=UserName,OU=Container,DC=Contoso,DC=Com");
            designer.AddOutput("Deleted Computer LDAP Path").AsString();
        }

        public void Execute(IOpalisRequest request, IOpalisResponse response)
        {
            string deletedComputerLDAPPath = request.Inputs["Comptuer LDAP Path"].AsString();
            string computerName = deletedComputerLDAPPath.Split(',')[0].Substring(10);
            string container = "LDAP://" + deletedComputerLDAPPath.Substring(11 + computerName.Length);

            DirectoryEntry dirEntry = new DirectoryEntry(container, credentials.UserName + "@" + credentials.Domain, credentials.Password);

            DirectoryEntry computer = dirEntry.Children.Find("CN=" + computerName, "computer");

            dirEntry.Children.Remove(computer);
            dirEntry.CommitChanges();
            dirEntry.Close();

            response.Publish("Deleted User LDAP Path", deletedComputerLDAPPath);
        }
    }
}


